package come.demo.widgets.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import come.demo.widgets.R;
import come.demo.widgets.multiplicity.HeaderFooterDataType;
import come.demo.widgets.multiplicity.MultipleAdapter;

public class MultipleListActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;

    MultipleTypeAdapter mAdapter;

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, MultipleListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multiple_list);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter = new MultipleTypeAdapter());
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.remove();
            }
        });
    }


    static class MultipleTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        static final int INDEX_TYPE0 = 0;
        static final int INDEX_TYPE1 = 1;
        MultipleAdapter mHelper = new MultipleAdapter();

        MultipleTypeAdapter() {
            final List<String> type0 = new ArrayList<>();
            for (int i = 0; i < 200; ++i) {
                type0.add(String.valueOf(i));
            }
            HeaderFooterDataType<String, String, String> dataType = new HeaderFooterDataType<>(INDEX_TYPE0, "title", type0, "footer");
            mHelper.add(INDEX_TYPE0, dataType);


        }

        public void add() {
            final List<String> type1 = new ArrayList<>();
            for (int i = 0; i < 20; ++i) {
                type1.add(String.valueOf(i));
            }
            HeaderFooterDataType<String, String, String> dataType1 = new HeaderFooterDataType<>(INDEX_TYPE1, "title", type1, "footer");
            mHelper.add(INDEX_TYPE1, dataType1);
            notifyDataSetChanged();
        }

        public void remove() {
//            final Object[] ss = {"100", "1000"};
//            mHelper.notifyItemMoved(1, 2);
//            notifyItemMoved(1, 2);
            mHelper.notifyItemRemoved(1);
            notifyItemRangeRemoved(1, 2);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final HeaderFooterDataType dataType = (HeaderFooterDataType) mHelper.findDataTypeByType(viewType);
            RecyclerView.ViewHolder result = null;
            switch (dataType.getTypeIndex()) {
                case INDEX_TYPE0:
                    if (viewType == dataType.getHeaderItemType()) {
                        result = new ViewHolder0(parent.getContext());
                    } else if (viewType == dataType.getDataItemsType()) {
                        result = new ViewHolder1(parent.getContext());
                    } else {
                        result = new ViewHolder2(parent.getContext());
                    }
                    break;
                case INDEX_TYPE1:
                    if (viewType == dataType.getHeaderItemType()) {
                        result = new ViewHolder3(parent.getContext());
                    } else if (viewType == dataType.getDataItemsType()) {
                        result = new ViewHolder4(parent.getContext());
                    } else {
                        result = new ViewHolder5(parent.getContext());
                    }
                    break;
            }
            return result;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final Object o = mHelper.getItem(position);
            if (holder instanceof ViewHolder0) {
                ((ViewHolder0) holder).setData((String) o);
            } else if (holder instanceof ViewHolder1) {
                ((ViewHolder1) holder).setData((String) o);
            } else if (holder instanceof ViewHolder2) {
                ((ViewHolder2) holder).setData((String) o);
            } else if (holder instanceof ViewHolder3) {
                ((ViewHolder3) holder).setData((String) o);
            } else if (holder instanceof ViewHolder4) {
                ((ViewHolder4) holder).setData((String) o);
            } else if (holder instanceof ViewHolder5) {
                ((ViewHolder5) holder).setData((String) o);
            }
        }


        @Override
        public int getItemCount() {
            return mHelper.getItemCount();
        }

        static class ViewHolder0 extends RecyclerView.ViewHolder {

            public ViewHolder0(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

        static class ViewHolder1 extends RecyclerView.ViewHolder {
            public ViewHolder1(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

        static class ViewHolder2 extends RecyclerView.ViewHolder {

            public ViewHolder2(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

        static class ViewHolder3 extends RecyclerView.ViewHolder {

            public ViewHolder3(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

        static class ViewHolder4 extends RecyclerView.ViewHolder {

            public ViewHolder4(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

        static class ViewHolder5 extends RecyclerView.ViewHolder {

            public ViewHolder5(Context context) {
                super(new TextView(context));
            }

            public void setData(String val) {
                ((TextView) itemView).setText(val);
            }

        }

    }
}
